package com.gitee.aurora.server.dao.impl;

import com.gitee.aurora.core.comm.AuroraJdbcTemplate;
import com.gitee.aurora.server.dao.RulesLogResultDao;
import com.gitee.aurora.server.vo.RulesLogResultVo;
import com.gitee.aurora.comm.Constant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import java.util.List;

@Service
public class RulesLogResultDaoImpl  implements RulesLogResultDao {
    @Autowired
    private AuroraJdbcTemplate auroraJdbcTemplate;
    @Override
    public List<RulesLogResultVo> queryRulesLogResultList(RulesLogResultVo rulesLogResultVo) {
        List<Object> params = new ArrayList<>();
        StringBuilder sql  =new StringBuilder( " SELECT r.ID id, r.`COLUMN` `column`,r.`VALUE` value,r.DEPTCODE deptcode,r.COLUMNNAME columnname,r.STATUS status, r.TIP tip ,r.INSERTTIME inserttime,r.TABLENAME tablename,p.S_CODE code,p.S_CODE_NAME codename, r.TABLEID  tableid FROM  RULES_LOG_RESULT r left join TAB_PTCJ_CONFIG p on p.PT_IP=r.DEPTCODE where 1=1");
        if(StringUtils.isNotEmpty(rulesLogResultVo.getCode())){
            String [] lcode=rulesLogResultVo.getCode().split(",");
            sql.append(" and  p.S_CODE in ( ");
            for (int i=0;i<lcode.length;i++){
                if(i<lcode.length-1){
                    sql.append("?");
                    params.add(lcode[i]);
                    sql.append(",");

                }else{
                    sql.append("?");
                    params.add(lcode[i]);
                }
            }
            sql.append(" ) ");
        }
        if(StringUtils.isNotEmpty(rulesLogResultVo.getTablename())){
            sql.append(" and  r.TABLENAME=?");
            params.add(rulesLogResultVo.getTablename());
        }
        if(StringUtils.isNotEmpty(rulesLogResultVo.getTableid())){
            sql.append(" and  r.TABLEID=?");
            params.add(rulesLogResultVo.getTableid());
        }
        sql.append(" and  r.STATUS=? ");
        params.add(Constant.status);
        if(rulesLogResultVo.getList().size()>0&&rulesLogResultVo.getList()!=null){
            sql.append(" and  r.INSERTTIME in (  ");
            for (int i=0;i<rulesLogResultVo.getList().size();i++){
                if(i<rulesLogResultVo.getList().size()-1){
                    sql.append("?");
                    params.add(rulesLogResultVo.getList().get(0));
                    sql.append(",");

                }else{
                    sql.append("?");
                    params.add(rulesLogResultVo.getList().get(0));
                }
            }
            sql.append(" ) ");

        }
        return  auroraJdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(RulesLogResultVo.class), params.toArray());
    }

    @Override
    public List<RulesLogResultVo> queryMaxRulesLogResultSj(RulesLogResultVo rulesLogResultVo) {
        List<Object> params = new ArrayList<>();
        StringBuilder sql  =new StringBuilder( " SELECT MAX(r.INSERTTIME) inserttime FROM  RULES_LOG_RESULT r left join TAB_PTCJ_CONFIG p on p.PT_IP=r.DEPTCODE where 1=1");
        if(rulesLogResultVo.getCode()!=null){
            sql.append(" and  p.S_CODE =?");
            params.add(rulesLogResultVo.getCode());
        }
        if(rulesLogResultVo.getTablename()!=null){
            sql.append(" and  r.TABLENAME=?");
            params.add(rulesLogResultVo.getTablename());
        }
        sql.append(" and  r.STATUS=? ");
        params.add(Constant.status);
        return auroraJdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(RulesLogResultVo.class), params.toArray());
    }
}
